package com.xuhuan.demo.remote;

import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * @Author huan.xu
 * @Date 2022/10/17 20:02
 * @Description 接口调用工具类
 */
@Slf4j
public class RemoteUtil {

/**
 * 发起 get 请求
 *
 * @param restTemplate RestTemplate 对象
 * @param url          请求 url
 * @param paramMap     请求参数
 * @return
 */
public static String sendGet(RestTemplate restTemplate, String url,
    Map<String, Object> paramMap, String authorization) {
    log.info("--------发起get请求--------");
    log.info("请求地址[{}]", url);
    log.info("请求参数[{}]", paramMap);

    StringBuilder urlBuilder = new StringBuilder(url);

    if (!paramMap.isEmpty()) {
        boolean flag = false;
        for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
            String key = entry.getKey();
            String value = String.valueOf(entry.getValue());
            if (!flag) {
                urlBuilder.append("?");
                flag = true;
            } else {
                urlBuilder.append("&");
            }
            urlBuilder.append(key).append("=").append(value);
        }
    }

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("Authorization", authorization);
    HttpEntity<Map<String, Object>> request = new HttpEntity<>(null, headers);

    log.info("完整的get请求url[{}]", urlBuilder.toString());

    ResponseEntity<String> responseEntity = restTemplate
        .exchange(urlBuilder.toString(), HttpMethod.GET, request, String.class);
    HttpStatus statusCode = responseEntity.getStatusCode();
    log.info("请求状态[{}]", statusCode);
    if (HttpStatus.OK != statusCode) {
        throw new RuntimeException("发起get请求出错");
    }
    String body = responseEntity.getBody();
    log.info("请求结果[{}]", body);
    return body;
}

/**
 * 发起 post 请求
 *
 * @param restTemplate RestTemplate 对象
 * @param url          请求 url
 * @param paramMap     请求参数
 * @return
 */
public static String sendPost(RestTemplate restTemplate, String url,
    Map<String, Object> paramMap, String authorization) {
    log.info("--------发起post请求--------");
    log.info("请求地址[{}]", url);
    log.info("请求参数[{}]", paramMap);

    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
    headers.add("Authorization", authorization);
    HttpEntity<Map<String, Object>> request = new HttpEntity<>(paramMap, headers);

    ResponseEntity<String> responseEntity = restTemplate
        .postForEntity(url, request, String.class);
    HttpStatus statusCode = responseEntity.getStatusCode();
    log.info("请求状态[{}]", statusCode);
    if (HttpStatus.OK != statusCode) {
        throw new RuntimeException("发起post请求出错");
    }
    String body = responseEntity.getBody();
    log.info("请求结果[{}]", body);
    return body;
}
}
